<?php

/**
 * Define the base class for the services.
 *
 * @package    urCronPlugin
 * @subpackage services
 * @author     Marc Weistroff <marc.weistroff@gmail.com>
 * @version    $Id: urCronServiceBase.class.php 18 2009-06-10 15:35:38Z flambeur $
 */

class urCronServiceBase
{
  protected $feed = null;
  protected $feedId = null;
  protected $feedUri = null;

  /**
   * Return an array with the service name && the class name
   * @abstract
   * @static
   * @return array
   *
   */
  static function getServiceName()
  {
    return array();
  }

  /**
   * Set the feed id.
   * @param integer $feedId
   */
  public function setFeedId($feedId)
  {
    $this->feedId = $feedId;
  }

  /**
   * Set the feed uri.
   * @param String $feedUri
   */
  public function setFeedUri($feedUri)
  {
    $this->feedUri = $feedUri;
  }

  public function getFeedUri()
  {
    return $this->feedUri;
  }

  public function getFeedId()
  {
    return $this->feedId;
  }

  public function getFeed()
  {
    return $this->feed;
  }

  /**
   * 
   */
  public function doCronJob()
  {
    if(!$this->getFeedUri())
      throw new sfException('no feed uri specified. Weird!');
    if(!$this->getFeedId())
      throw new sfException('no feed id specified. Weird!');

    $this->feed = new SimpleXMLElement($this->getFeedUri(), null, true);

    $channel = $this->feed->xpath('channel');
    if(count($channel) > 0)
    {
      $this->doCronJobRss();
    }
    
    $feed = $this->feed->xpath('feed');
    if(count($feed) > 0)
    {
      $this->doCronJobAtom();
    }
  }

  /**
   *
   * @param SimpleXMLElement $entry
   * @return Array 
   */
  protected function parseAtomEntry(SimpleXMLElement $entry)
  {
    $isPublished = sfConfig::get('app_urcron_autopublish', true);
    $publishedDate = date('Y-m-d H:i:s', strtotime((string)$entry->published));
    $link = (string)$entry->xpath("link[@rel='alternate']")->attribute('href');

    $data = array('original_id'                 => (string)$entry->id,
                  'original_content'            => (string)$entry->content,
                  'original_title'              => (string)$entry->title,
                  'original_summary'            => (string)$entry->summary,
                  'is_published'                => $isPublished,
                  'original_link'               => $link,
                  'original_published_date'     => $publishedDate,
                  'ur_cron_feed_id'             => $this->feedId,
                  'original_author'             => (string)$entry->author->name
                );
                
    return $data;
  }

  /**
   * Just in case of in a derivated class, one of the feed type is not supported...
   */
  protected function doCronJobAtom()
  {
     throw new sfException('The ATOM feed is not currently supported. Sorry pal.');
  }
  
  protected function doCronJobRss()
  {
     throw new sfException('The RSS feed is not currently supported. Sorry pal.');
  }
  
  /**
   * Parse SimpleXMLElement $item for <category> tags and return an array with the term.
   * 
   * @param SimpleXMLElement $item
   * @return Array parsed tags
   */
  public function parseTags(SimpleXMLElement $item)
  {
    $tags = array();
    foreach($item->category as $cat)
    {
      $tags[] = (string)$cat['term'];
    }
    return $tags;
  }
}
?>
